package br.com.globalcode.teste;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class OlaMundo extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        response.setContentType("text/html");
        // Obtencao do canal para envio de dados para o cliente
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head><title>Ola Mundo Servlet</title></head>");
        out.println("<body>");
        out.println("Ola Mundo - requisicao recebida via GET");
        out.println("<br/>Data atual: " + new java.util.Date());
//        
//    String title = "Servlet Example: Showing Request Headers";
//    out.println("<BODY BGCOLOR=\"#FDF5E6\">\n" +
//                "<H1 ALIGN=CENTER>" + title + "</H1>\n" +
//                "<B>Request Method: </B>" +
//                request.getMethod() + "<BR>\n" +
//                "<B>Request URI: </B>" +
//                request.getRequestURI() + "<BR>\n" +
//                "<B>Request Protocol: </B>" +
//                request.getProtocol() + "<BR><BR>\n" +
//                "<TABLE BORDER=1 ALIGN=CENTER>\n" +
//                "<TR BGCOLOR=\"#FFAD00\">\n" +
//                "<TH>Header Name<TH>Header Value");
    out.println(montaLista(request));    
    Enumeration headerNames = request.getHeaderNames();
    while(headerNames.hasMoreElements()) {
      String headerName = (String)headerNames.nextElement();
      out.println("<TR><TD>" + headerName);
      out.println("    <TD>" + request.getHeader(headerName));
    }        
        
        out.println("</body>");
        out.println("</html>");
        // Vai imprimir na console tomcat
        System.out.println("Catalogo Produtos: requisicao via GET");
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        response.setContentType("text/html");
        // Obtencao do canal de comunicacao de envio de dados para o cliente
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head><title>Ola Mundo Servlet</title></head>");
        out.println("<body>");
        out.println("Ola Mundo - requisicao recebida via POST");
        out.println("<br/>Data atual: " + new java.util.Date());
        
        out.println("</body>");
        out.println("</html>");
        // Vai imprimir na console do tomcat
        System.out.println("Catalogo Produtos: requisicao via POST");
        out.println(montaLista(request)); 
    }
    
    public String montaLista(HttpServletRequest request){
        
        String title = "Servlet Example: Showing Request Headers";
        return ("<BODY BGCOLOR=\"#FDF5E6\">\n" +
                "<H1 ALIGN=CENTER>" + title + "</H1>\n" +
                "<B>Request Method: </B>" +
                request.getMethod() + "<BR>\n" +
                "<B>Request URI: </B>" +
                request.getRequestURI() + "<BR>\n" +
                "<B>Request Protocol: </B>" +
                request.getProtocol() + "<BR><BR>\n" +
                "<TABLE BORDER=2 ALIGN=CENTER>\n" +
                "<TR BGCOLOR=\"#FFAD00\">\n" +
                "<TH>Header Name<TH>Header Value");
        
    }
}